﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;

namespace baseClass.controls
{
    public partial class emProjectDetail : common.control.baseUserControl
    {
        private BindingSource myDataSource = null;
        public emProjectDetail()
        {
            InitializeComponent();
            SetMaxLength();
            projectCodeEd.isToUpperCase = true;
            projTitleEd.Enabled = false;
            projTitleEd.BackColor = common.settings.sysColorDisableBG; 
        }
        private void SetMaxLength()
        {
            data.baseDS.emProjEmployeeDataTable tbl = new data.baseDS.emProjEmployeeDataTable();
            workDescEd.MaxLength = tbl.workDescriptionColumn.MaxLength;
        }

        public delegate data.baseDS.emProjectRow onNeedProjectData(object sender, string projectCode);
        public event onNeedProjectData myOnNeedProjectData = null;
        private data.baseDS.emProjectRow GetProject(string projectCode)
        {
            if (this.myOnNeedProjectData == null) return null;
            return this.myOnNeedProjectData(this, projectCode);
        }
        
        public virtual void Init() 
        {
            workRoleCb.LoadData();
        }
        public void SetDataSource(System.Windows.Forms.BindingSource dataSrc)
        {
            this.myDataSource = dataSrc;
            data.baseDS.emProjEmployeeDataTable tbl = (data.baseDS.emProjEmployeeDataTable)dataSrc.DataSource;
            this.projectCodeEd.DataBindings.Clear();
            this.projectCodeEd.DataBindings.Add(new System.Windows.Forms.Binding("Text", dataSrc, tbl.projectCodeColumn.ColumnName, true));

            this.startDateEd.DataBindings.Clear();
            this.startDateEd.DataBindings.Add(new System.Windows.Forms.Binding("Text", dataSrc, tbl.startDateColumn.ColumnName, true));

            this.endDateEd.DataBindings.Clear();
            this.endDateEd.DataBindings.Add(new System.Windows.Forms.Binding("Text", dataSrc, tbl.endDateColumn.ColumnName, true));

            this.workRoleCb.DataBindings.Clear();
            this.workRoleCb.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", dataSrc, tbl.workRoleColumn.ColumnName, true));

            this.workDescEd.DataBindings.Clear();
            this.workDescEd.DataBindings.Add(new System.Windows.Forms.Binding("Text", dataSrc, tbl.workDescriptionColumn.ColumnName, true));
        }
        
        public virtual bool CheckData()
        {
            if (this.myDataSource.Current == null) return true;
            bool retVal = true;
            ClearNotifyError();
            if (projectCodeEd.Text.Trim() == "")
            {
                NotifyError(projectCodeLbl);
                retVal = false;
            }
            if (startDateEd.myDateTime == common.Consts.constNullDate)
            {
                NotifyError(startDateLbl);
                retVal = false;
            }
            if (workRoleCb.myValue == "")
            {
                NotifyError(workRoleLbl);
                retVal = false;
            }
            if (workDescEd.Text.Trim() == "")
            {
                NotifyError(workDescLbl);
                retVal = false;
            }
            return retVal;
        }
        public virtual void LockEdit(bool state)
        {
            if (myDataSource.Current == null) state = true; 
            projectCodeEd.ReadOnly = state;
            startDateEd.ReadOnly = state;
            endDateEd.ReadOnly = state;
            workDescEd.ReadOnly = state;
            workRoleCb.Enabled = !state;
        }
        public virtual void SetFocus()
        {
            this.Focus();
            projectCodeEd.Focus();
        }

        private void projectCodeEd_Validated(object sender, EventArgs e)
        {
            try
            {
                data.baseDS.emProjectRow projRow = GetProject(projectCodeEd.Text);
                projTitleEd.Text = "";
                if (projRow != null)
                {
                    projTitleEd.Text = projRow.title;
                }
            }
            catch (Exception er)
            {
                this.ErrorHandler(sender, er);
            }
        } 
    }
}
